package pl.nazaweb.samplesandtutorials.adapters;

import java.util.List;

import pl.nazaweb.samplesandtutorials.R;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class ShopListAdapter extends ArrayAdapter<ShopListItem> {

	private Activity context;
	private List<ShopListItem> items;

	static class ViewHolder {
		public TextView itemName;
		public TextView itemPrice;
		public TextView itemCurrency;
	};

	public ShopListAdapter(Activity context, List<ShopListItem> items) {
		super(context, R.layout.shop_list_element, items);
		this.context = context;
		this.items = items;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		View rowView = convertView;
		if (rowView == null) {
			LayoutInflater layoutInflater = context.getLayoutInflater();
			rowView = layoutInflater.inflate(
					R.layout.shop_list_element, null, true);
			viewHolder = new ViewHolder();
			viewHolder.itemName = (TextView) rowView
					.findViewById(R.id.shop_list_item_name);
			viewHolder.itemCurrency = (TextView) rowView
					.findViewById(R.id.shop_list_item_currency);
			viewHolder.itemPrice = (TextView) rowView
					.findViewById(R.id.shop_list_price);
			rowView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) rowView.getTag();
		}
		viewHolder.itemName.setText(items.get(position).getItemName());
		viewHolder.itemPrice.setText(items.get(position).getItemPrice() + "");
		return rowView;
	}

}
